/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package documenteditor.controller.legado;

import documenteditor.controller.legado.exceptions.NonexistentEntityException;
import documenteditor.entidade.model.legado.Finaprazo;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;

/**
 *
 * @author gleidson.moura
 */
public class FinaprazoJpaController {

    public FinaprazoJpaController() {
        emf = Persistence.createEntityManagerFactory("FidyCardPU_Legado");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Finaprazo finaprazo) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(finaprazo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Finaprazo finaprazo) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            finaprazo = em.merge(finaprazo);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = finaprazo.getIdFinaprazo();
                if (findFinaprazo(id) == null) {
                    throw new NonexistentEntityException("The finaprazo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Finaprazo finaprazo;
            try {
                finaprazo = em.getReference(Finaprazo.class, id);
                finaprazo.getIdFinaprazo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The finaprazo with id " + id + " no longer exists.", enfe);
            }
            em.remove(finaprazo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Finaprazo> findFinaprazoEntities() {
        return findFinaprazoEntities(true, -1, -1);
    }

    public List<Finaprazo> findFinaprazoEntities(int maxResults, int firstResult) {
        return findFinaprazoEntities(false, maxResults, firstResult);
    }

    private List<Finaprazo> findFinaprazoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Finaprazo as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Finaprazo findFinaprazo(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Finaprazo.class, id);
        } finally {
            em.close();
        }
    }

    public List<Finaprazo> findFinaprazoByCodigo(Date dataVenda, Long codigo, int maxResults, int firstResult, boolean all) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Finaprazo as o WHERE o.codigo = :codigo AND o.dataVenda > :dataVenda");
            q.setParameter("codigo", codigo);
            q.setParameter("dataVenda", dataVenda);
            if (all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public int getFinaprazoCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from Finaprazo as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
